package org.come.Jpanel;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.come.entity.Baby;
import org.come.entity.Pal;
import org.come.model.PalData;
import org.come.until.AnalysisString;

import com.tool.btn.PartnerBtn;
import com.tool.image.ImageMixDeal;
import com.tool.pet.PetProperty;
import com.tool.role.RoleData;
import com.tool.tcp.NewPart;
import com.tool.tcp.SpriteFactory;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;
import org.come.until.StringUtil;

/**
 * @author HGC<br>
 * @time 2019年12月20日 上午9:53:20<br>
 * @class 类名:PartnerTeamJpanel 伙伴队伍面板 <br>
 */
public class PartnerTeamJpanel extends JPanel {

    /**
     * 字段名:btnWar 参战按钮<br>
     * 字段名:btnResistance 抗性按钮<br>
     */
    private PartnerBtn btnWar, btnResistance;

    /**
     * 字段名:btnArrAck 调整操作按钮<br>
     */
    private PartnerBtn[] btnArrAck;
    private String[] btnArrStr;

    private JLabel[] labMessage;

    public PartnerTeamJpanel() {
        setOpaque(false);
        setLayout(null);

        getBtnWar();
        getBtnResistance();
        getBtnArrAck();
        getLabMessage();
    }

    /**
     * 展示或者隐藏调整操作按钮
     *
     * @param type
     */
    public void showBtnArr(boolean type) {
        for (int i = 0; i < btnArrAck.length; i++) {
            btnArrAck[i].setVisible(type);
        }
    }

    /**
     * 隐藏操作按钮
     */
    public boolean hideBtnArr() {
        if (btnArrAck[0].isVisible()) {
            showBtnArr(false);
            return true;
        }
        return false;
    }

    public void showPalMessage(Pal pal, PalData palData) {
        String zz = null;
        if (pal != null) {//修复助战伙伴点击不显示信息，多开点击未激活卡死的问题
            zz = growLvl(pal.getGrow());
        }
        labMessage[0].setText(palData.getName());
        labMessage[1].setText(palData.getTrait());
        Integer grade = RoleData.getRoleData().getLoginResult().getGrade();
        String lvl = AnalysisString.lvl(grade);
        labMessage[2].setText(lvl + "级");
        labMessage[3].setForeground(getColors()[growColor(pal == null ? 0 : pal.getGrow())]);
        labMessage[3].setText((pal == null ? growLvl(0) : zz));
        int[] palHMASp = PetProperty.getPalHMASp(pal, palData);
        labMessage[4].setText(palHMASp[0] + StringUtil.EMPTY_STRING);
        labMessage[5].setText(palHMASp[1] + StringUtil.EMPTY_STRING);
        labMessage[6].setText(palHMASp[2] + StringUtil.EMPTY_STRING);
        labMessage[7].setText(palHMASp[3] + StringUtil.EMPTY_STRING);

        if (part == null) {
            part = SpriteFactory.createPart(palData.getSkin(), 7, 1, null);
        } else {
            part = SpriteFactory.setPart(part, 1, palData.getSkin());
        }
        if (pal != null) {
            if (pal.getSummoning() != null) {
                if (petpart == null) {
                    petpart = SpriteFactory.createPart(pal.getSummoning().getSummoningskin(), 7, 1, null);
                } else {
                    petpart = SpriteFactory.setPart(petpart, 1, pal.getSummoning().getSummoningskin());
                }
            } else {
                petpart = null;
            }
            if (pal.getLingbao() != null) {
                if (lingBaopart == null) {
                    lingBaopart = SpriteFactory.createPart(pal.getLingbao().getSkin(), 7, 1, null);
                } else {
                    lingBaopart = SpriteFactory.setPart(lingBaopart, 1, pal.getLingbao().getSkin());
                }
            } else {
                lingBaopart = null;
            }
            if (pal.getBaby() != null) {
                Baby baby = pal.getBaby();
                String nan = "100001";
                String nv = "100002";
                if (baby.getXiaoxin() > 1000) {
                    nan = "100007";
                    nv = "100008";
                    if (baby.getXiaoxin() > 2000) {
                        nan = "100009";
                        nv = "100006";
                    }
                }
                String model = baby.getChildSex() == 0 ? nan : nv;
                if (babypart == null) {
                    babypart = SpriteFactory.createPart(model, 7, 1, null);
                } else {
                    babypart = SpriteFactory.setPart(babypart, 1, model);
                }
            } else {
                babypart = null;
            }
        }
    }

    private Color[] colors;

    public int growColor(double grow) {
        // 1 1.1 1.22 1.36
        // 资质平平 出类拔萃 神通广大 万中无一
        // 绿 蓝 紫 橙
        if (grow == 1) {
            return 0;
        } else if (grow == 1.5) {
            return 1;
        } else if (grow == 2.0) {
            return 2;
        } else if (grow == 2.5) {
            return 3;
        }
        return 0;
    }

    public String growLvl(double grow) {
        // 1 1.1 1.22 1.36
        // 资质平平 出类拔萃 神通广大 万中无一
        // 绿 蓝 紫 橙
        if (grow == 1) {
            return "资质平平";
        } else if (grow == 1.5) {
            return "出类拔萃";
        } else if (grow == 2.0) {
            return "神通广大";
        } else if (grow == 2.5) {
            return "万中无一";
        }
        return "资质平平";
    }

    private ImageIcon icon;

    public static NewPart part;
    public static NewPart petpart;
    public static NewPart lingBaopart;
    public static NewPart babypart;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B304.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 334, 290, this);
            if (part != null) {
                part.draw(g, 80, 200, 0, ImageMixDeal.userimg.getTime());
            }
        } else {
            //
            if (icon == null) {
                icon = new ImageIcon("img/xy2uiimg/S150.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 334, 290, this);
            if (part != null) {
                part.draw(g, 80, 200, 0, ImageMixDeal.userimg.getTime());
            }
        }
    }

    public PartnerBtn getBtnWar() {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            if (btnWar == null) {
                btnWar = new PartnerBtn("inkImg/newbutton/B20.png", 1, UIUtils.COLOR_BLACK, UIUtils.TEXT_HY16, "参战", 4);
                btnWar.setBounds(10, 254, 59, 24);
                this.add(btnWar);
            }
        } else {
            //
            if (btnWar == null) {
                btnWar = new PartnerBtn("inkImg/hongmu/6026.png", 1, UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, "参战", 4);
                btnWar.setBounds(10, 254, 60, 26);
                this.add(btnWar);
            }
        }
        return btnWar;
    }

    public void setBtnWar(PartnerBtn btnWar) {
        this.btnWar = btnWar;
    }

    public PartnerBtn getBtnResistance() {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
            //
            if (btnResistance == null) {
                btnResistance = new PartnerBtn("inkImg/newbutton/B20.png", 1, UIUtils.COLOR_BLACK, UIUtils.TEXT_HY16, "抗性",
                        5);
                btnResistance.setBounds(90, 254, 59, 24);
                this.add(btnResistance);
            }
        } else {
            //
            if (btnResistance == null) {
                btnResistance = new PartnerBtn("inkImg/hongmu/6026.png", 1, UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY16, "抗性",
                        5);
                btnResistance.setBounds(90, 254, 60, 26);
                this.add(btnResistance);
            }
        }
        return btnResistance;
    }

    public void setBtnResistance(PartnerBtn btnResistance) {
        this.btnResistance = btnResistance;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public PartnerBtn[] getBtnArrAck() {
        if (btnArrAck == null) {
            btnArrAck = new PartnerBtn[4];
            String style = MyIsif.getStyle();
            if ("水墨UI".equals(style)) {
                //
                for (int i = 0; i < btnArrAck.length; i++) {
                    btnArrAck[i] = new PartnerBtn("inkImg/newbutton/B20.png", 1, UIUtils.COLOR_BLACK, UIUtils.TEXT_HY16,
                            "", 10);
                    btnArrAck[i].setBounds(10, 254 - ((i + 1) * 26), 59, 24);
                    btnArrAck[i].setVisible(false);
                    this.add(btnArrAck[i]);
                }
            } else {
                //
                for (int i = 0; i < btnArrAck.length; i++) {
                    btnArrAck[i] = new PartnerBtn("inkImg/hongmu/6026.png", 1, UIUtils.COLOR_BTNPUTONG2, UIUtils.TEXT_HY88,
                            "", 10);
                    btnArrAck[i].setBounds(10, 254 - ((i + 1) * 26), 60, 26);
                    btnArrAck[i].setVisible(false);
                    this.add(btnArrAck[i]);
                }
            }
        }
        return btnArrAck;
    }

    public void setBtnArrAck(PartnerBtn[] btnArrAck) {
        this.btnArrAck = btnArrAck;
    }

    public String[] getBtnArrStr() {
        if (btnArrStr == null) {
            btnArrStr = new String[5];
            btnArrStr[0] = "1号";
            btnArrStr[1] = "2号";
            btnArrStr[2] = "3号";
            btnArrStr[3] = "4号";
            btnArrStr[4] = "休息";
        }
        return btnArrStr;
    }

    public void setBtnArrStr(String[] btnArrStr) {
        this.btnArrStr = btnArrStr;
    }

    public NewPart getPart() {
        return part;
    }

    public void setPart(NewPart part) {
        PartnerTeamJpanel.part = part;
    }

    public JLabel[] getLabMessage() {
        if (labMessage == null) {
            labMessage = new JLabel[8];
            for (int i = 0; i < labMessage.length; i++) {
                labMessage[i] = new JLabel();
                labMessage[i].setForeground(Color.white);
                labMessage[i].setBounds(221, 13 + i * 28, 111, 16);
                labMessage[i].setFont(UIUtils.TEXT_FONT1);
                this.add(labMessage[i]);
            }
        }
        return labMessage;
    }

    public void setLabMessage(JLabel[] labMessage) {
        this.labMessage = labMessage;
    }

    public Color[] getColors() {
        if (colors == null) {
            colors = new Color[4];
            colors[0] = Color.green;
            colors[1] = Color.cyan;
            colors[2] = Color.ORANGE;
            colors[3] = Color.red;
        }
        return colors;
    }

    public void setColors(Color[] colors) {
        this.colors = colors;
    }

}
